package com.spera.test;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.spera.aliyunUtil.CreateOSSClient;
import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UpdateMap;
import com.spring.security.web.dao.CommonMapper;
import com.spring.tools.SpringContextHolder;
import org.springframework.beans.factory.annotation.Value;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SperaUtil {
	
	private static CommonMapper commonMapper = SpringContextHolder.getBean(CommonMapper.class);

	public static List<Map<String, Object>> getCommonDataList(String type) {
		Map<String, Object> m = new HashMap<String, Object>();
		if (!ApplicationUtils.isNotNull(type)) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "请输入类型");
			return null;
		}
		UpdateMap sel = new UpdateMap("base_type_code");
		sel.addWhere("type", type);
		List<Map<String, Object>> list = commonMapper.select(sel);
		List<Map<String, Object>> nlist = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> d : list) {
			d.remove("id");
			nlist.add(d);
		}
		return nlist;
	}
	
	
	@Value("${ipad.user_url}")
	protected String oaUrl;
	
	
	public static String getOaUrl() {
		return JedisCont.getProParm("ipad.user_url");
	}
	
	public static String getProParm(String parm) {
		return JedisCont.getProParm(parm);
	}

	public static String imageFileUpOss(String bucketName, String key, InputStream fis, Long fileSize) throws Exception {
		OSSClient client = CreateOSSClient.getInstance().getOSSClient();
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.setContentLength(fileSize.longValue());
		objectMeta.setContentType("image/jpeg");
		client.putObject(bucketName, key, fis, objectMeta);
		return "http://" + bucketName + ".oss-cn-hangzhou.aliyuncs.com/" + key;
	}

	public static String fileUpOss(String bucketName, String key, InputStream fis, String contentType,Long fileSize) throws Exception {
		OSSClient client = CreateOSSClient.getInstance().getOSSClient();
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.setContentLength(fileSize.longValue());
		objectMeta.setContentType(contentType);
		client.putObject(bucketName, key, fis, objectMeta);
		return "http://" + bucketName + ".oss-cn-hangzhou.aliyuncs.com/" + key;
	}
}
